package searchteam.client.ui.widgets;

import searchteam.client.beansMirrors.PaperMirror;
import searchteam.client.beansMirrors.PersonMirror;
import searchteam.client.beansMirrors.TeamMirror;
import searchteam.client.ui.handlers.LinkClickHandler;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class PersonMirrorDisplay extends MirrorDisplay{

	private PersonMirror bean;
	private Display display;
	
	public PersonMirrorDisplay(Display d, PersonMirror mirror) {
		bean = mirror;
		display = d;
		this.setText(bean.getName() + " " + bean.getFirstname());
	}
	
	public PersonMirror getBean() {
		return bean;
	}
	
	public Panel display() {
		Grid panel = new Grid(9, 2);
		
		Button buttonEdit = new Button("Modifier");
		buttonEdit.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				new UpdatePersonPopUp(display, bean);
			}
		});
		
		VerticalPanel papersPanel = new VerticalPanel();
		TeamMirror team = bean.getTeam();
		TeamMirror ownedTeam = bean.getOwnedTeam();
		
		panel.setWidget(0, 0, new Label("Nom : "));
		panel.setWidget(0, 1, new Label(bean.getName()));
		panel.setWidget(1, 0, new Label("Prenom : "));
		panel.setWidget(1, 1, new Label(bean.getFirstname()));
		panel.setWidget(2, 0, new Label("Tel : "));
		panel.setWidget(2, 1, new Label(bean.getPhone()));
		panel.setWidget(3, 0, new Label("Mail : "));
		panel.setWidget(3, 1, new Label(bean.getMail()));
		panel.setWidget(4, 0, new Label("Chef de : "));
		if(ownedTeam != null) {
			TeamMirrorDisplay t = new TeamMirrorDisplay(display, ownedTeam);
			panel.setWidget(4, 1, t);
			t.addClickHandler(new LinkClickHandler(display.getMainUI(), t));
		}
		panel.setWidget(5, 0, new Label("Equipe : "));
		if(team != null) {
			TeamMirrorDisplay t = new TeamMirrorDisplay(display, team);
			panel.setWidget(5, 1, t);
			t.addClickHandler(new LinkClickHandler(display.getMainUI(), t));
		}
		panel.setWidget(6, 0, new Label("Papiers : "));
		panel.setWidget(6, 1, papersPanel);
		panel.setWidget(8, 1, buttonEdit);
		
		for(PaperMirror p : bean.getPapers()) {
			PaperMirrorDisplay paper = new PaperMirrorDisplay(display, p);
			paper.addClickHandler(new LinkClickHandler(display.getMainUI(), paper));
			papersPanel.add(paper);
		}
		
		return panel;
	}

	public int getId() {
		return bean.getId();
	}

}
